package manongfanshen.memory_model.volatile_atomic;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.stream.IntStream;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * @author ChangLiang
 * @date 2020/3/19
 */
public class MainTestV0 {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * volatile: ensure visibility and Orderly, not atomic
     */
    volatile int value = 0;

    @Test
    @DisplayName("test volatile atomic")
    public void test(){

        int initActiveCount = Thread.activeCount();
        logger.info("initActiveCount:{}", initActiveCount);

        // start 10 thread
        IntStream.rangeClosed(1,10).forEach(i->new Thread(String.valueOf(i)){
            @Override
            public void run() {
                for (int j = 0; j < 1000; j++) {
                    value++;
                }
            }
        }.start());

        // ensure 10 thread finish
        while (Thread.activeCount() > initActiveCount) {
            Thread.yield();
        }

        logger.info("value:{}", value);
        assertThat(value).isEqualTo(10000);
    }
}
